<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            width: 300px;
            height: 500px;
            border: 1px solid red;
            margin: 100px;
            position: relative;
            overflow: hidden;
        }

        .content {
            padding: 5px 18px 5px 5px;
            position: absolute;
            top: 0;
            left: 0;

        }

        .scroll {
            width: 18px;
            height: 100%;
            position: absolute;
            top: 0;
            right: 0;
            background-color: #eee;
        }

        .bar {
            height: 100px;
            width: 100%;
            position: absolute;
            top: 0;
            left: 0;
            background-color: red;
            border-radius: 10px;
            cursor: pointer;
        }
    </style>
</head>
<body>
<div class="box" id="box">
    <div class="content" id="content">
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头
        床前明月光啊，明月光，疑是地上霜啊，举头嘿嘿

    </div><!--文字内容-->
    <div id="scroll" class="scroll"><!--装滚动条的层-->
        <div class="bar" id="bar"></div><!--滚动条-->
    </div>
</div>

<script>
    function getById(id) {
        return document.getElementById(id);
    }

    var box = getById('box');
    var content = getById('content');
    var scroll = getById('scroll');
    var bar = getById('bar');
    //设置滚动条的高度
    var barH = scroll.offsetHeight * box.offsetHeight / content.offsetHeight;
    bar.style.height = parseInt(barH) + "px";

    //设置滚动条的高度
    //滚动条的高/装滚动条的div的高=box的高/文字div的高
    //滚动条的高=装滚动条的div的高*box的高/文字div的高
    var height = scroll.offsetHeight * box.offsetHeight / content.offsetHeight;
    bar.style.height = parseInt(height) + "px";

    //滚动条拖动
    bar.onmousedown = function (e) {
        var spaceY = e.clientY - bar.offsetTop;
        document.onmousemove = function (ev) {
            var y = ev.clientY - spaceY;
            y = y < 0 ? 0 : y;
            y = y > scroll.offsetHeight - bar.offsetHeight ? scroll.offsetHeight - bar.offsetHeight : y;
            bar.style.top = y + "px";
            //设置文字不被选中
            window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
            //文字内容移动
            var moveY = y * (content.offsetHeight - box.offsetHeight) / (scroll.offsetHeight - bar.offsetHeight);
            content.style.marginTop = -moveY + "px";
        }
    };
    document.onmouseup = function (ev) {
        document.onmousemove = null;
    }


</script>

</body>
</html>